package aptech.ad02.database;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.example.danangtravel.Tbshopping;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

public class DataAccess {

	// Table tbATM
	public static final String TABLE_ATM = "tbATM";
	public static final String ATM_ID = "ID";
	public static final String ATM_Name = "Name";
	public static final String ATM_Adrress = "Adrress";
	public static final String ATM_Longitude = "Longitude";
	public static final String ATM_Latitude = "Latitude";

	// Table tbTaxi
	public static final String TABLE_Taxi = "tbTaxi";
	public static final String Taxi_Name = "Name";
	public static final String Taxi_Phone = "Phone";
	public static final String Taxi_Serve = "Serve";

	// Table tbFoodies and Table tbHotel: Dung chung
	public static final String TABLE_Foodies = "tbFoodies";
	public static final String TABLE_Hotel = "tbHotel   ";
	public static final String COL_ID = "ID";
	public static final String COL_Name = "Name";
	public static final String COL_Adrress = "Adrress";
	public static final String COL_Phone1 = "Phone1";
	public static final String COL_Phone2 = "Phone2";
	public static final String COL_Information = "Information";
	public static final String COL_Type = "Type";
	public static final String COL_Image = "Image";
	public static final String COL_Rating = "Rating";
	public static final String COL_Longitude = "Longitude";
	public static final String COL_Latitude = "Latitude";

	// Table tbShopping
	public static final String TABLE_Shopping = "tbShopping";
	public static final String Shopping_ID = "ID";
	public static final String Shopping_Name = "Name";
	public static final String Shopping_Adrress = "Adrress";
	public static final String Shopping_Information = "Information";
	public static final String Shopping_Type = "Type";
	public static final String Shopping_Specialty = "Specialty";
	public static final String Shopping_Longitude = "Longitude";
	public static final String Shopping_Latitude = "Latitude";

	// Table tbTravelPoint
	public static final String TABLE_TravelPoint = "tbTravelPoint";
	public static final String TravelPoint_ID = "ID";
	public static final String TravelPoint_Name = "Name";
	public static final String TravelPoint_Adrress = "Address";
	public static final String TravelPoint_Information = "Information";
	public static final String TravelPoint_Image1 = "Image1";
	public static final String TravelPoint_Image2 = "Image2";
	public static final String TravelPoint_Image3 = "Image3";
	public static final String TravelPoint_Image4 = "Image4";
	public static final String TravelPoint_Image5 = "Image5";
	public static final String TravelPoint_Type = "Type";
	public static final String TravelPoint_Longitude = "Longitude";
	public static final String TravelPoint_Latitude = "Latitude";

	// Database fields
	private SQLiteDatabase database;
	private MySQLiteHelper dbHelper;
	private Cursor cursor;
	// Colume tbShopping
	public String[] allcolume_tbshopping = { Shopping_ID, Shopping_Name,
			Shopping_Adrress, Shopping_Information, Shopping_Type,
			Shopping_Specialty, Shopping_Longitude, Shopping_Latitude };

	public DataAccess(Context context) throws IOException {
		try {
			dbHelper = new MySQLiteHelper(context);
			dbHelper.createDataBase();
		} catch (IOException ioe) {
			throw ioe;
		} catch (SQLException sqle) {
			throw sqle;
		}
		database = dbHelper.getReadableDatabase();

	}

	public void open() throws SQLException {
		dbHelper.openDataBase();
	}

	public void close() {
		dbHelper.close();
	}

	// code for table tbATM

	// code for table tbTaxi

	// code for table tbFoodies

	// code for table tbHotel

	//************************** code for table tbShopping
	public List<Tbshopping> getAllShopping_sieuthi() {
		// Dung rawquery cho de~ 
		String sql = "SELECT * FROM " + TABLE_Shopping + " WHERE Type = 1";
		List<Tbshopping> list = new ArrayList<Tbshopping>();
			//Cursor cursor = database.query(TABLE_Shopping, allcolume_tbshopping,
			//				null, null, null, null, null);
		 Cursor cursor = database.rawQuery(sql, null);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {

			Tbshopping tbshoping = new Tbshopping();
			tbshoping.setName(cursor.getString(1));
			tbshoping.setAdress(cursor.getString(2));
			list.add(tbshoping);
			cursor.moveToNext();
		}
		cursor.close();
		return list;
	}
	public List<Tbshopping> getAllShopping_cho() {
		// Dung rawquery cho de~ 
		String sql = "SELECT * FROM " + TABLE_Shopping + " WHERE Type = 2";
		List<Tbshopping> list = new ArrayList<Tbshopping>();
			//Cursor cursor = database.query(TABLE_Shopping, allcolume_tbshopping,
			//				null, null, null, null, null);
		 Cursor cursor = database.rawQuery(sql, null);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {

			Tbshopping tbshoping = new Tbshopping();
			tbshoping.setName(cursor.getString(1));
			tbshoping.setAdress(cursor.getString(2));
			list.add(tbshoping);
			cursor.moveToNext();
		}
		cursor.close();
		return list;
	}
	public List<Tbshopping> getAllShopping_cuahang() {
		// Dung rawquery cho de~ 
		String sql = "SELECT * FROM " + TABLE_Shopping + " WHERE Type = 3";
		List<Tbshopping> list = new ArrayList<Tbshopping>();
			//Cursor cursor = database.query(TABLE_Shopping, allcolume_tbshopping,
			//				null, null, null, null, null);
		 Cursor cursor = database.rawQuery(sql, null);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {

			Tbshopping tbshoping = new Tbshopping();
			tbshoping.setName(cursor.getString(1));
			tbshoping.setAdress(cursor.getString(2));
			list.add(tbshoping);
			cursor.moveToNext();
		}
		cursor.close();
		return list;
	}
	//********************************* end code for tabale tbShopping
	// code for table tbTravelPoint

}
